package com.aidong.myprofile.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.myprofile.biz.MyAppointmentBiz;
import com.aidong.myprofile.entity.Appointment;
import com.aidong.pay.config.util.OrdersStatus;
import com.aidong.util.PageUtil;
import com.aidong.util.ReadPropertiesUtil;
import com.aidong.util.SortUtil;
import com.aidong.util.bus.CodeConsts;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 我的预约列表
 * @author java
 * @version 1.0    
 *  2015.07.05
 */
@ParentPackage("json")
@Namespace("/")
public class MyAppointmentAction implements ModelDriven<Appointment> {

	private Appointment appointment;
	private int goodsType;
	@Override
	public Appointment getModel() {
		if(appointment == null){
			appointment = new Appointment();
		}
		return appointment;
	}
	
	private MyAppointmentBiz appointmentBiz; // 预约查询service
	
	private Map<String, Object> map;
	
	private int pageCurrent; // 当前页

	public void setPageCurrent(int pageCurrent) {
		this.pageCurrent = pageCurrent;
	}

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	public void setGoodsType(int goodsType) {
		this.goodsType = goodsType;
	}

	/**
	 * 预约查询service
	 * @param appointmentBiz
	 */
	@Autowired
	public void setAppointmentBiz(MyAppointmentBiz appointmentBiz) {
		this.appointmentBiz = appointmentBiz;
	}

	/**
	 * 场馆预约列表
	 * @return
	 */
	@Action(value="getstadiumlist",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getStadiumList(){
		map = new HashMap<String, Object>();
		try {
			int idongId = appointment.getIdongId(); // 预约人爱动号
			if(idongId > 0){
				// 我的场馆预约列表
				Map<String, Object> pageMap = PageUtil.page(20, pageCurrent);
				pageMap.put("idongId", idongId);
				List<Map<String, Object>> stadiums = appointmentBiz.getStadiumList(pageMap);
				if(stadiums.size() > 0){
					SortUtil.appointStatusSort(stadiums);
					for (Map<String, Object> stadium : stadiums) {
						stadium.put("brandUrl", ReadPropertiesUtil.getIMGREAD(stadium.get("brandUrl")));//预约场馆图片
						String status = stadium.get("status") + "";
						String pmAm = stadium.get("pmAm").toString();
						String sysdate = new SimpleDateFormat("HH:mm").format(new Date());
						if(!"".equals(status) && status != null){
							int sta = Integer.parseInt(status);
							if(sta == 2){
								stadium.put("status", "预约成功");
							}else if(("上午".equals(pmAm) && sysdate.compareTo("10:00") > 0) || ("下午".equals(pmAm) && sysdate.compareTo("16:00") > 0)){
								stadium.put("status", "已完成");
							}else if(sta == 4){
								stadium.put("status", "已取消");
							}else if(sta == 5){
								stadium.put("status", "");
							}
						}
						stadium.put("startDate", stadium.get("startDate") + " " + stadium.get("pmAm"));
						stadium.remove("count");
						stadium.remove("pmAm");
					}
				}
				map.put("code", 1);
				map.put("result", stadiums);
				map.put("pageCurrent", pageMap.get("pageCurrent"));
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 课程预约列表
	 * @return
	 */
	@Action(value="getcourselist",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getCourseList(){
		map = new HashMap<String, Object>();
		try {
			int idongId = appointment.getIdongId(); // 预约人爱动号
			if(idongId > 0){
				// 我的课程预约列表
				Map<String, Object> pageMap = PageUtil.page(5, pageCurrent);
				pageMap.put("idongId", idongId);
				List<Map<String, Object>> courses = appointmentBiz.getCourseList(pageMap);
				/*List<Map<String, Object>> courseOrders = appointmentBiz.getCourseOrderList(pageMap);
				int ii = 0;
				List<Map<String, Object>> coursesResult = new ArrayList<Map<String, Object>>();
				for (Map<String, Object> coursesOrder : courseOrders) {
					for
				}*/
				for (Map<String, Object> course : courses) {
					//course.put("brandUrl", ReadPropertiesUtil.getIMGREAD() + course.get("brandUrl"));
					String signStatus = course.get("signStatus") + ""; // 预约状态
					String processId = course.get("processId") + ""; // 流程ID
					String orderStatus = course.get("orderStatus") + ""; // 订单状态
					String dataStatus = course.get("dataStatus") + ""; // 时间状态
					
					String status = OrdersStatus.payStatus(signStatus, processId,orderStatus,dataStatus);
					course.put("status", status);
					course.remove("signStatus");
					course.remove("processId");
					course.remove("orderStatus");
					course.remove("dataStatus");
					
					String subscribeDate = course.get("subscribeDate") + "";
					String startTime = course.get("startTime") + "";
					course.put("subscribeDate", subscribeDate + " " + startTime);
					
					course.remove("startTime");
					course.put("refund", 2);//1-可退，2-不可退
					course.put("brandUrl", ReadPropertiesUtil.getCourseImg(course.get("gpPic")));
					course.put("gpPic", ReadPropertiesUtil.getCourseImg(course.get("gpPic")));//课程图片
					
					//ii ++;
				}
				map.put("code", 1);
				map.put("result", courses);
				map.put("pageCurrent", pageMap.get("pageCurrent"));
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 私教预约列表
	 * @return
	 */
	@Action(value="getcoachlist",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getCoachList(){
		map = new HashMap<String, Object>();
		try {
			int idongId = appointment.getIdongId(); // 预约人爱动号
			if(idongId > 0){
				// 私教预约列表 
				Map<String, Object> pageMap = PageUtil.page(20, pageCurrent);
				pageMap.put("idongId", idongId);
				List<Map<String, Object>> coachs = appointmentBiz.getCoachList(pageMap);
				SortUtil.appointStatusSort(coachs);
				for (Map<String, Object> coach : coachs) {
					String status = coach.get("status") + "";
					if(!"".equals(status) && status != null){
						int sta = Integer.parseInt(status);
						if(sta == 1){
							coach.put("status", "待确认");
						}else if(sta == 2){
							coach.put("status", "预约成功");
						}else if(sta == 3){
							coach.put("status", "已完成");
						}else if(sta == 4){
							coach.put("status", "已取消");
						}else if(sta == 5){
							coach.put("status", "");
						}
					}
					coach.put("startDate", coach.get("startDate") + " " + coach.get("pmAm"));
					coach.remove("count");
					coach.remove("pmAm");
					coach.put("brandUrl", ReadPropertiesUtil.getCourseImg(coach.get("coachPhoto")));
					coach.put("coachPhoto", ReadPropertiesUtil.getCourseImg(coach.get("coachPhoto")));//私教头像
					
				}
				map.put("code", 1);
				map.put("result", coachs);
				map.put("pageCurrent", pageMap.get("pageCurrent"));
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 活动报名列表
	 * @return
	 */
	@Action(value="getcampaignlist",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getCampaignList(){
		map = new HashMap<String, Object>();
		try {
			int idongId = appointment.getIdongId(); // 报名人爱动号
			if(idongId > 0){
				// 活动报名列表
				Map<String, Object> pageMap = PageUtil.page(20, pageCurrent);
				pageMap.put("idongId", idongId);
				List<Map<String, Object>> campaigns = appointmentBiz.getCampaignList(pageMap);
				for (Map<String, Object> campaign : campaigns) {
					String tickNumber = campaign.get("movieTicketNum") + "";
					if("null".equals(tickNumber) || tickNumber == null){
						campaign.put("movieTicketNum", "");
					}
					String signStatus = campaign.get("signStatus") + ""; // 预约状态0
					String processId = campaign.get("processId") + ""; // 流程ID2
					String orderStatus = campaign.get("orderStatus") + ""; // 订单状态6
					String dataStatus = campaign.get("dataStatus") + ""; // 时间状态
					
					String status = OrdersStatus.payStatus(signStatus, processId == null ? "" : processId, orderStatus == null ? "" : orderStatus, dataStatus);
					campaign.put("status", status);
					campaign.remove("signStatus");
					campaign.remove("processId");
					campaign.remove("orderStatus");
					campaign.remove("dataStatus");
					campaign.put("refund", 2);//1可退，2不可退
					campaign.put("brandUrl", ReadPropertiesUtil.getCourseImg(campaign.get("bigPic")));//活动
					campaign.put("bigPic", ReadPropertiesUtil.getCourseImg(campaign.get("bigPic")));
					
				}
				
				map.put("code", 1);
				map.put("result", campaigns);
				map.put("pageCurrent", pageMap.get("pageCurrent"));
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 餐饮订购列表
	 * @return
	 */
	@Action(value="getfoodmeallist",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getFoodMealList(){
		map = new HashMap<String, Object>();
		try {
			int idongId = appointment.getIdongId(); // 报名人爱动号
			if(idongId > 0){
				// 活动报名列表
				Map<String, Object> pageMap = PageUtil.page(20, pageCurrent);
				pageMap.put("idongId", idongId);
				pageMap.put("goodsType", goodsType);
				if (goodsType == 3) {
					pageMap.put("brandDict", "foodBrandDict");
				}else if (goodsType == 6) {
					pageMap.put("brandDict", "nourishmentBrandDict");
				}else if (goodsType ==7) {
					pageMap.put("brandDict", "equipBrandDict");
				}
				
				List<Map<String, Object>> buyFoods = appointmentBiz.getFoodMealList(pageMap);
				for (Map<String, Object> food : buyFoods) {
					if (goodsType == 8) {
						food.put("brandName", food.get("brandNameMatch"));
						food.put("quantity", food.get("quantityMatch"));
						food.put("picUrl", ReadPropertiesUtil.getIMGREAD(food.get("picUrlMatch")));
						food.put("buyId", food.get("buyIdMatch"));
						food.put("foodId", food.get("campaignId"));
						food.put("pickUpWay", food.get("pickUpWayMatch"));
					}else {
						food.put("picUrl", ReadPropertiesUtil.getIMGREAD(food.get("picUrl")));
					}
					
					String payType = food.get("payType").toString();//订单支付类型
					String buyStatus = food.get("buyStatus") + ""; // 预约状态
					String buyStatusMatch = food.get("buyStatusMatch") + ""; // 预约状态
					String processId = food.get("processId") + ""; // 流程ID
					String orderStatus = food.get("orderStatus") + ""; // 订单状态
					String status = "";
					if (goodsType != 8) {
						status = OrdersStatus.payStatus(buyStatus, processId == null ? "" : processId, orderStatus == null ? "" : orderStatus, "");
						food.put("status", status);
					}else {
						status = OrdersStatus.payStatus(buyStatusMatch, processId == null ? "" : processId, orderStatus == null ? "" : orderStatus, "");
						food.put("status", status);
					}
					
					food.remove("buyStatus");
					food.remove("processId");
					food.remove("orderStatus");
					food.remove("dataStatus");
					food.remove("picUrlMatch");
					food.remove("brandNameMatch");
					food.remove("quantityMatch");
					food.remove("campaignId");
					food.remove("pickUpWayMatch");
					food.put("freight", CodeConsts.Freight);// 运费--需要配置
					/*if (goodsType == 6) {//营养品
						if (!status.equals("1")) {
							food.put("refund", 2);//1可退，2不可退
						}else {
							food.put("refund", 1);//1可退，2不可退
						}
					}else {
						food.put("refund", 2);//1可退，2不可退
					}*/
					food.put("refund", 2);//1可退，2不可退
				}
				
				map.put("code", 1);
				map.put("result", buyFoods);
				map.put("pageCurrent", pageMap.get("pageCurrent"));
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 预约取消
	 * @return
	 */
	@Action(value="cancleappointment",results={@Result(name="succ",type="json",params={"root","map"})})
	public String editAppointment(){
		map = new HashMap<String, Object>();
		try {
			int idongId = appointment.getIdongId(); // 预约人爱动号
			int subscribeId = appointment.getSubscribeId(); // 预约ID
			String appointType = appointment.getAppointType(); // 预约类型
			if(idongId > 0 && subscribeId > 0 && (!"".equals(appointType) && appointType != null)){
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("idongId", idongId);
				paraMap.put("subscribeId", subscribeId);
				if("activity".equals(appointType)){ // 活动
					appointmentBiz.editCampaignCancle(paraMap);
				}else if("group".equals(appointType)){ // 团操课程
					appointmentBiz.editCourseCancle(paraMap);
				}else if("coach".equals(appointType)){ // 私教
					appointmentBiz.editCoachCancle(paraMap);
				}else if("stadium".equals(appointType)){ // 场馆
					appointmentBiz.editStadiuCancle(paraMap);
				}
				map.put("code", 1);
				map.put("msg", "取消成功");
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}